package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.TestDateDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import java.util.*;

@Service(interfaceClass = TestDateService.class)
@Transactional
public class TestDateServiceImpl implements TestDateService{
    @Autowired
    private TestDateDao testDateDao;
    @Override
    public List<Map<String, Object>> setDateFind(Map map) throws Exception{
        Object startDate=null;
        Object endDate=null;
        String startDate1 = map.get("startDate").toString();
        String endDate1 = map.get("endDate").toString();
        if (StringUtils.isNotBlank(startDate1)){
          startDate=  map.get("startDate");
        }
        if (StringUtils.isNotBlank(endDate1)){
            endDate=  map.get("endDate");
        }
        if (startDate!=null &&startDate!="" && endDate!=null && endDate!="") {
            Date start = DateUtils.parseString2Date(startDate.toString());
            Date end = DateUtils.parseString2Date(endDate.toString());
            if (end.before(start)) {
                Date date = new Date();
                date = end;
                end = start;
                start = date;
                map.put("startDate", start);
                map.put("endDate", end);
            }
        }
        List<Map<String, Object>> stringObjectMap = testDateDao.setDateFind(map);
        return stringObjectMap;
    }

    @Override
    public List<Map<String, Object>> setAutoDateFind(int id) throws Exception{
        Date today = DateUtils.getToday();
        Calendar instance = Calendar.getInstance();
        instance.setTime(today);
        instance.add(Calendar.DAY_OF_WEEK,7);
        Date time = instance.getTime();
        String s = DateUtils.parseDate2String(time);
        String s1 = DateUtils.parseDate2String(DateUtils.getToday());
        Map<String,Object> map=new HashMap<>();
        map.put("startDate",s1);
       map.put("endDate",s);
        map.put("id",id);
        List<Map<String, Object>> maps = testDateDao.setDateFind(map);
        return maps;
    }



}
